package com.ndp.fb.walle.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ndp.fb.rdb.api.EventNotificationMessageRdbService;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.message.EventNotificationMessage;
import com.ndp.fb.walle.business.MessageService;
import com.ndp.fb.walle.model.vo.eventnotificationmessage.EventNotificationMessageVo;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hermen.he on 2015/7/28.
 */
@Component
public class MessageServiceImpl implements MessageService {

    @Reference
    private EventNotificationMessageRdbService messageRdbService;

    @Override
    public Long updateReadStatusById(String id) {
        EventNotificationMessage eventNotificationMessage = messageRdbService.findUnReadEventNotificationMessageById(id);
        if(eventNotificationMessage !=null ){
           return messageRdbService.updateReadStatusById(id);
        }
        return 0l;
    }

    @Override
    public Pager<EventNotificationMessageVo> findUnReadEventNotificationMessageByUserId(String userId, Integer pageNow, Integer pageSize) {
        Pager<EventNotificationMessage> pager = messageRdbService.findUnReadEventNotificationMessageByUserId(userId, pageNow, pageSize);
        List<EventNotificationMessage> list = pager.getResultList();
        List<EventNotificationMessageVo> voList = new ArrayList<>();
        Pager<EventNotificationMessageVo> voPager = new Pager<>();
        for (EventNotificationMessage message : list) {
            EventNotificationMessageVo vo = new EventNotificationMessageVo();
            vo.setChannel(message.getChannel());
            vo.setEventType(message.getEventType());
            vo.setExpiredTime(message.getExpiredTime());
            vo.setIsNeedFunctionJump(message.isNeedFunctionJump());
            vo.setIsRead(message.getIsRead());
            vo.setUrl(message.getUrl() != null?message.getUrl():"");
            vo.setBody(message.getBody());
            vo.setId(message.getId());
            vo.setReceiver(message.getReceiver());
            vo.setSender(message.getSender());
            vo.setSendTime(message.getSendTime());
            vo.setEventKey(message.getEventKey());
            vo.setEventValues(message.getEventValues());
            switch (message.getStatus()){
                case DELETEED:
                    vo.setStatus(0);break;
                case NORMAL:
                    vo.setStatus(1);break;
                default:break;
            }
            vo.setSendTime(message.getSendTime());
            voList.add(vo);
        }
        voPager.setResultList(voList);
        voPager.setPageIndex(pager.getPageIndex());
        voPager.setPageSize(pager.getPageSize());
        voPager.setTotalSize(pager.getTotalSize());
        return voPager;
    }


}
